<?php
namespace app\api\controller;

use think\Controller;
use think\Db;
use app\api\model\News ;
use app\api\model\Preg ;
use app\api\model\Talent;
use app\api\model\Service as service_list;
use think\Paginator;

class Service extends Index
{
    /**首页列表**/
    public function index(){
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 20;
        $index = (isset($_GET['index']) && !empty($_GET['index'])) ? $_GET['index'] : array('neq',0);
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $search = (isset($_GET['name']) && $_GET['name']) ? trim($_GET['name']) : null;
        $type = (isset($_GET['type']) && $_GET['type']) ? trim($_GET['type']) : array('neq', 0);
        $status = (isset($_GET['status']) && $_GET['status']) ? trim($_GET['status']) : array('neq', 0);
        $order = (isset($_GET['order']) && $_GET['order']) ? trim($_GET['order']) : "w.edit_time desc";
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']): 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ?(strtotime($_GET['logmax']) + 24*60*60 -1): time();
        $minmoney= (isset($_GET['minmoney']) && $_GET['minmoney']) ?$_GET['minmoney']: 0;
        $maxmoney= (isset($_GET['maxmoney']) && $_GET['maxmoney']) ?$_GET['maxmoney']: 0;
        if($maxmoney){
            $where['w.price']  = array(array('egt', $minmoney),array('elt', $maxmoney),'and');
        }else{
            $where['w.price']  = array('egt', $minmoney);
        }
        $where['w.goods_name'] = array('like', "%$search%");
        $where['w.status'] = $status;
        $where['w.type'] = $type;
        $where['w.classify']=$index;
        $where['w.add_time']  = array(array('egt', $logmin),array('elt', $logmax),'and');
        //调用模型
        $condition1 = 'w.type=t.id';
        $type1='zgh_service_type t';
        $type2='zgh_shop_information i';
        $condition2 = 'w.s_id=i.id';
        $where['i.static']=1;
        $where['w.state']=1;
        $service_list = new service_list('service_market');
        $count = $service_list->count($type1,$type2,$where,$condition1,$condition2);
        // dump($count);exit;
        $fields='w.*,i.name as shop_name,t.t_name';
        $data = $service_list->service_list($type1,$type2,$where,$condition1,$condition2,$fields,$number,$page,$order);
        //分页
        $Page = new \app\api\model\Page($count,$number);
        $render = $Page->render;
        //html代码
        $res = model('Common')->page_data($count, $page, $number);
        $res['count'] = $count;
        $res['data'] = $data;
        $res['render'] = $render;
        if (isset($res) && !empty($res)) {
            $this->_jsonp('200', '新闻列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '新闻列表数据获取失败', null);
        }
    }
    /**获取分类**/
    public function get_type(){
        $news=new News('service_type');
        $where['pid']=input('get.type');
        $where['level']=2;
        $data=$news->table_list($where);
        if(!empty($data)&&$data){
            $this->_jsonp('200','获取分类成功',$data);
        }else{
            $this->_jsonp('1000','获取分类失败',null);
        }
    }

    /**服务类商品审核**/
    public function goods_del(){
        $true = auth();
        if ($true == 2) {
            $id = input('get.id');
            $data['status'] = input('get.status');
            $data['reason'] = input('get.reason');
            $where['id'] = explode(",", $id);
            for ($i = 0; $i < count($where['id']); $i++) {
                $res = db('service_market')->where('id', $where['id'][$i])->update($data);
            }
            if (isset($res) || !empty($res)) {
                $this->_jsonp('200', '成功', null);
            } else {
                $this->_jsonp('1000', '失败', null);
            }
        } else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }
    /**服务类商品详情**/
    public function goods_detail(){
        $where['w.id'] = input('get.id');
        $condition1 = 'w.classify=c.id';
        $type1='zgh_service_type c';
        $type2='zgh_shop_information i';
        $condition2 = 'w.s_id=i.id';
        $type3='zgh_service_type t';
        $condition3 = 'w.type=t.id';
        $news_list = new service_list('service_market');
        $fields='w.*,i.name as shop_name,i.uid,c.t_name,t.t_name as detail';
        $data = $news_list->service_detail($type1,$type2,$type3,$where,$condition1,$condition2,$condition3,$fields);
        if ($data) {
            $this->_jsonp('200',  '查询成功', $data);
        } else {
            $this->_jsonp('1000', '查询成功', null);
        }
    }

    /**前台操作***/
    /**随机服务类商品**/
    public function rand_goods(){
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 2;
        $index = (isset($_GET['index']) && !empty($_GET['index'])) ? $_GET['index'] : array('neq', 0);
        $type = (isset($_GET['type']) && $_GET['type']) ? trim($_GET['type']) : array('neq', 0);
        $order = (isset($_GET['order']) && $_GET['order']) ? trim($_GET['order']) : "w.edit_time desc";
        $where['w.status'] = 1;
        $where['w.state'] = 1;
        $where['w.classify']=$index;
        $where['w.type']=$type;
        $type2='zgh_shop_information i';
        $condition2 = 'w.s_id=i.id';
        $news_list = new service_list('service_market');
        $fields='w.id,w.goods_name,w.price,w.pic,w.rate,w.volume,i.name';
        $data=$news_list->rand_list($where,$fields,$number,$order,$type2,$condition2);
        if ($data) {
            $this->_jsonp('200',  '查询成功', $data);
        } else {
            $this->_jsonp('1000', '查询成功', null);
        }
    }
    public function rand_desc(){
       $sql="select id,goods_name,price,pic from (select * from zgh_service_market order by volume desc limit 2) tmp order by rand() limit 1";
        $res = Db::query($sql);
        dump($res);
    }
    /**服务类商品详情**/
    public function services_detail(){
        $where['w.id'] = input('get.id');  
        $where['w.state']=array('neq',3);
        $condition1 = 'w.classify=c.id';
        $type1='zgh_service_type c';
        $type2='zgh_shop_information i';
        $condition2 = 'w.s_id=i.id';
        $type3='zgh_service_type t';
        $condition3 = 'w.type=t.id';
        $news_list = new service_list('service_market');
        $fields='w.*,i.name as shop_name,i.uid as user_id,i.shop_volume,i.username,i.label,i.image,c.t_name,t.t_name as detail,i.id as shop_id';
        $res= $news_list->service_detail($type1,$type2,$type3,$where,$condition1,$condition2,$condition3,$fields);
        $data=$res;
        if($data){
            $map['s_id']=$res['s_id'];
            $map['status']=1;
            $map['state']=1;
            $res1 = $news_list->num($map);
            $trading['name']=$res['s_id'];
            $trading['status'] = 1;
            $trading['static'] = 2;
            $trading['is_show'] = 1;
            $trading_list=new service_list('market_trading');
            $res3 = $trading_list->num($trading);
            $data['service_count']=$res1;
            $data['trading_count']=$res3;
            $show['id']=array('in',$data['label']);
            $list = new service_list('shop_label');
            $res2 = $list->label($show,'name as label_name');
            $data['label']=$res2;
        }
        if ($res) {
            $this->_jsonp('200',  '查询成功', $data);
        } else {
            $this->_jsonp('1000', '查询成功', null);
        }
    }
    /**前台联系商家**/
    public function goods_chat(){
        $data['goods_id'] = input('get.goods_id');
        $data['content'] = htmlspecialchars(trim(input('get.content')));
        $data['from_uid'] = input('get.from_uid');
        $data['to_uid'] = input('get.to_uid');
        $data['reply_type'] = 2;
		$data['time'] = time();
        $data['index'] = 1;

        $where1['to_uid'] = $data['to_uid'];
        $where1['from_uid'] = $data['from_uid'];
        $where1['reply_type'] = 2;
        $data1['index'] = 0;
        db('goods_chat')->where($where1)->update($data1);
        $news_list=new Talent('goods_chat');
        $data = $news_list->talent_add($data);
        if ($data) {
            $this->_jsonp('200', '添加成功', null);
        } else {
            $this->_jsonp('1000',  '添加失败', null);
        }
    }
    /**前台收藏商品**/
    public function goods_collect(){
        $where['mid'] = input('get.id');//收藏的id
        $where['uid'] = input('get.uid');
        $where['type'] =2;
        $where['static'] = 0;
        $base = new News('collect');
        $like = $base->check_name($where);
        //dump($arr);
        if (empty($like)) {
            $map['id']=input('get.id');
            $where['time']=time();
            // dump($data);die;
            //收藏用户写入
            $like_add = Db::name("collect")->insert($where);
            $add = Db::name("service_market")->where($map)->setInc('collect');
            if ($like_add !== false) {
                $this->_jsonp('200', '商品收藏成功', null);
            } else {
                $this->_jsonp('1000', '商品收藏失败', null);
            }
        }else{
            $this->_jsonp('1001', '商品已收藏', null);
        }
    }
    /**前台收藏商品**/
    public function cancel_collect(){
        $where['id'] = input('get.id');//收藏的id
        $sheet = input('get.form');//收藏类型，例如news
        $map['uid'] = input('get.uid');
        $basename = input('get.sheet');//收藏所在的表类型
        $news_list = new News('user');
        $base = new News($basename);
        $like = $news_list->check_name($map, 'collect_id');
        $result = json_decode($like['collect_id'], true) ? json_decode($like['collect_id'], true) : array();
        //dump($result);
        if (empty($result[$sheet])) {
            $result[$sheet] = array();
        }
        $arr = $result[$sheet];
        //dump($arr);
        if (in_array($where['id'], $arr)) {
            foreach ($arr as $k => $v) {
                if ($where['id'] == $v) unset($arr[$k]);
            }
            $arr = array_merge($arr);
            $result[$sheet] = $arr;
            $data['collect_id'] = json_encode($result);
            //dump($data);die;
            $like_add = Db::name("user")->where($map)->update($data);
            $add = $base->minus($where, 'collect');
            if ($like_add !== false) {
                $this->_jsonp('200', '商品取消收藏成功', null);
            } else {
                $this->_jsonp('1000', '商品取消收藏失败', null);
            }
        }
    }
    /**前台添加购物车**/
    public function add_cart(){
        $data['uid']=input('get.uid');
        $data['goods_id']=input('get.goods_id');
        $data['status']=1;
        $news=new News('shop_cart');
        $id=$news->check_name($data,'id');
        if($id){
            $this->_jsonp('1001','已添加购物车',null);
        }else{
            $talent=new Talent('shop_cart');
            $time['time']=time();
            $result=array_merge($data,$time);
            $res=$talent->talent_add($result);
            if($res&&!empty($res)){
                $this->_jsonp('200','成功',null);
            }else{
                $this->_jsonp('1000','失败',null);
            }
        }
    }
    /**购物车列表**/
    public function cart_list(){
        $where['w.uid']=input('get.uid');
        $where['w.status']=1;
        $condition1 = 'w.goods_id=m.id';
        $type1='zgh_service_market m';
        $type2='zgh_shop_information i';
        $condition2 = 'm.s_id=i.id';
        $type3='zgh_service_type t';
        $condition3 = 'm.classify=t.id';
        $service=new service_list('shop_cart');
        $fields='m.goods_name,m.price,m.s_id,m.pic,w.*,t.t_name,i.name,i.id as shop_id';
        $order='w.time desc';
        $where['i.static']=1;//店铺存在店铺禁用，其所有商品状态变化，商品下架也是
        $where['w.status']=1;
        $data=$service->cart_list($type1,$type2,$type3,$where,$condition1,$condition2,$condition3,$fields,$order);
//        $field='sum(m.price) as total';
//        $num=$service->cart_list($type1,$type2,$type3,$where,$condition1,$condition2,$condition3,$field,$order);
//        $result['total']=$num[0]['total'];
        $result['data']=$data;
       // $result['count']=count($data);
        $result['shop_id']=$service->cart_sort($type1,$type2,$type3,$where,$condition1,$condition2,$condition3,'m.s_id,i.name','m.s_id',$order);
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '新闻列表数据获取成功', $result);
        } else {
            $this->_jsonp('1000', '新闻列表数据获取失败', null);
        }
    }
    /*购物车商品移入收藏夹**/
    public function cart_collect(){
        $id=input('get.id');
        $show['id']=array('in',$id);
        $goods_id=Db::name('shop_cart')->where($show)->column('goods_id');
        $map['uid'] = input('get.uid');
        $base = new News('service_market');
        $where['uid']=input('get.uid');
        $where['static']=0;
        $where['type']=2;
        $arr=Db::name('collect')->where($where)->column('mid');
        if(empty($arr)){
            $start = $goods_id;
        }else{
            foreach ($goods_id as $k=> $v) {
                foreach($arr as $key=> $value){
                    if($value==$v){
                        unset($goods_id[$key]);
                    }
                }
            }
            $start=$goods_id;
        }
        $data_arr=array_merge($start);
        $data = array();
        foreach ($data_arr as $k=> $v){
            $data[]  = [
                'mid' => $data_arr[$k],
                'uid' => input('get.uid'),
                'type' => 2,
                'time' => time()
            ] ;
        }
        $update['status']=2;
        $l_id['id']=array('IN',implode(',',$data_arr));
        $goods=Db::name('shop_cart')->where($show)->update($update);
        if(!empty($data_arr)){
            Db::name('service_market')->where($l_id)->setInc('collect');
            $result   =  Db::name('collect')->insertAll($data);
        }else{
            $result=1;
        }
        if ($result !== false) {
            $this->_jsonp('200', '商品收藏成功', null);
        } else {
            $this->_jsonp('1000', '商品收藏失败', null);
        }
    }
    /*购物车商品删除**/
    public function cart_del(){
        $id=input('get.id');
        $news_list = new News('shop_cart');
        $data['status']=2;
        $res= $news_list->news_del($id,$data);
        if ($res['res']) {
            $this->_jsonp('200', $res['name'] . '成功', null);
        } else {
            $this->_jsonp('1000', $res['name'] . '失败', null);
        }
    }
    /*最近收藏**/
    public function recent_collect(){
        $where['a.uid']=input('get.uid');
        $where['a.type'] =2;
        $where['a.static'] = 0;
        $type1='zgh_service_market w';
		$condition1 = 'w.id=a.mid';
		$type2='zgh_shop_information i';
		$condition2 = 'w.s_id=i.id';
		
		$fields='w.id,w.goods_name,w.price,w.pic,w.rate,w.volume,i.name';
		$data=Db::name('collect')->alias('a')->field($fields)->join($type1,$condition1)->join($type2,$condition2)->where($where)->limit(5)->order('a.id desc')->select();
		//$data=$news_list->rand_list($map,$fields,5,'',$type2,$condition2);
		if ($data) {
			$this->_jsonp('200',  '查询成功', $data);
		} else {
			$this->_jsonp('1000',  '查询失败', null);
		}
        
    }
    /*最近浏览**/
    public function recent_view(){
        $id=input('get.id');
        $start_id=explode(",", $id);
        $news_list = new service_list('service_market');
        $where['w.id'] = input('get.id');
		$where['w.state'] = 1;
		$where['w.status'] = 1;
        $condition1 = 'w.classify=c.id';
        $type1='zgh_service_type c';
        $type2='zgh_shop_information i';
        $condition2 = 'w.s_id=i.id';
        $type3='zgh_service_type t';
        $condition3 = 'w.type=t.id';
        $fields='w.*,i.name as shop_name,i.shop_volume,i.username,i.label,i.image,c.t_name,t.t_name as detail';
        $data=array();
        foreach($start_id as $key=>$v){
            $where['w.id']=$v;
            $res= $news_list->service_detail($type1,$type2,$type3,$where,$condition1,$condition2,$condition3,$fields);
            if(!empty($res)){
                $data[]=  $res;
            }
        }
        if ($data) {
            $this->_jsonp('200',  '查询成功', $data);
        } else {
            $this->_jsonp('1000',  '查询失败', null);
        }
    }
    //评论列表
    public function order_comment_list(){
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $id=input('get.id');
        $where['b.g_id']=$id;
		$where['b.status']=1;
        $index = (isset($_GET['index']) && !empty($_GET['index'])) ? $_GET['index'] : array('neq',0);
        $where['b.sheet']=1;
        $where['b.state']=$index;
        $news_list = new News('order_comment');
        $type1='zgh_user u';
        $condition1='b.buyer_id=u.uid';
        $fields='b.*,u.head_img,u.username as buyer_name';
        $data=$news_list->two_list($where, $type1, $condition1, 10, $page, $fields);
        $count =$news_list->count($where, 'b');
		//屏蔽关键字
		$match = new Preg();
		foreach($data as $key=>$val){
                $html=$match->replace($val['content']);
				$data[$key]['content']=$html;
            }
			//
        //分页
        $Page = new \app\api\model\Page($count,10);
        $render = $Page->render;
        //$res = model('Common')->page_data($count, $page, 10);
        $where1['g_id']= $id;
        $where1['state']= 1;
		$where1['status']= 1;
        $num1=Db::name('order_comment')->where($where1)->count();
        $where2['g_id']= $id;
        $where2['state']= 2;
		$where2['status']= 1;
        $num2=Db::name('order_comment')->where($where2)->count();
        $where3['g_id']= $id;
        $where3['state']= 3;
		$where3['status']= 1;
        $num3=Db::name('order_comment')->where($where3)->count();
        $res['num1'] = $num1;
        $res['num2'] = $num2;
        $res['num3'] = $num3;
        $res['count'] = $count;
        $res['data'] = $data;
        $res['render'] = $render;
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '评论列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '评论列表数据获取失败', null);
        }
    }
    /***店铺中心商品列表接口***/
    public function serviceGoods_list(){
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $where['w.s_id']=input('get.id');//商店店铺id
        $where['w.state']=input('get.state');//上架状态
        $where['w.status']=input('get.status');//审核状态
        $news_list = new News('service_market');
        $count =$news_list->count($where, 'w');
        $type1='zgh_service_type t';
        $condition1='w.buyer_id=t.id';
        $fields='w.*,t.t_name';
        $data=$news_list->two_list($where, $type1, $condition1, 10, $page, $fields);
        //分页
        $Page = new \app\api\model\Page($count,10);
        $render = $Page->render;
        $res['count'] = $count;
        $res['data'] = $data;
        $res['render'] = $render;
        if (isset($data) && !empty($data)) {
            $this->_jsonp('200', '评论列表数据获取成功', $res);
        } else {
            $this->_jsonp('1000', '评论列表数据获取失败', null);
        }
    }
    /**发布服务类商品**/
    public function serviceGoods_add(){
        $data['goods_name']=input('post.goods_name');
        $data['s_id']=input('post.shop_id');
        $data['classify']=input('post.classify');
        $data['content']=input('post.content');
        $data['price']=input('post.price');
        $data['type']=input('post.type');
        $data['status']=2;//商品状态未审核
        $data['state']=2;//商品状态未上架
        $data['add_time']=time();
        $data['edit_time']=time();
        $news_list=new News();
        if(!empty($_FILES)){
            $files=request()->file('img');
            $img=$news_list->upload($files,'Images/serviceGoods');
            $data['img_url']=$img;
            if(!$data['img_url']){
                $this->json('1000','图片上传失败',null);
            }
        }
        $talent=new Talent('service_market');
        $res=$talent->talent_add($data);
        if($res&&!empty($res)){
            $this->json('200','商品发布成功',null);
        }else{
            $this->json('1000','商品发布失败',null);
        }
    }
    /**修改服务类商品**/
    public function serviceGoods_edit(){
        $where['id'] = input('get.id');
        $data['goods_name']=input('post.goods_name');
        $data['s_id']=input('post.shop_id');
        $data['classify']=input('post.classify');
        $data['content']=input('post.content');
        $data['price']=input('post.price');
        $data['type']=input('post.type');
        $data['status']=2;//商品状态未审核
        $data['state']=2;//商品状态未上架
        $data['edit_time']=time();
        $news_list=new News();
        if(!empty($_FILES)){
            $files=request()->file('img');
            $img=$news_list->upload($files,'Images/serviceGoods');
            $data['img_url']=$img;
            if(!$data['img_url']){
                $this->json('1000','图片上传失败',null);
            }
        }
        $talent=new Talent('service_market');
        $res=$talent->talent_edit($where,$data);
        if($res&&!empty($res)){
            $this->json('200','商品发布成功',null);
        }else{
            $this->json('1000','商品发布失败',null);
        }
    }
    /**前台商家服务类商品上下架**/
    public function serviceGoods_del(){
        $id = input('get.id');
        $data['state'] = input('get.state');
        $news_list = new News('service_market');
        $res = $news_list->news_del($id, $data);
        if ($res['res']) {
            $this->_jsonp('200', $res['name'] . '成功', null);
        } else {
            $this->_jsonp('1000', $res['name'] . '失败', null);
        }
    }
}